/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.pitkley.jocreatulary;

import java.io.*;
import java.util.*;

import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.json.JSONArray;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

/**
 *
 * @author pit
 */
public class Jocreatulary_Controller {

    private Jocreatulary_GUI theGUI;
    private ArrayList<Vocable> vocab = new ArrayList();
    private Vocable v;
    private Document dom;

    Jocreatulary_Controller(Jocreatulary_GUI pGUI) {
        theGUI = pGUI;
        v = new Vocable();
    }

    public void addVocable() {
        vocab.add(v);
        v = new Vocable();
        /*Vocable v = new Vocable();
        v.setNativeWord(pVocableToTranslate);
        v.setTranslation(pTranslation);
        v.setAlternativeTranslation(pAlternativeTranslation);

        vocab.add(v);*/
    }

    public void updateVocable(int index, String pVocableToTranslate, String pTranslation, String pAlternativeTranslation) {
        /*Vocable v = (Vocable) vocab.get(index);
        v.setNativeWord(pVocableToTranslate);
        v.setTranslation(pTranslation);
        v.setAlternativeTranslation(pAlternativeTranslation);*/
    }

    public Vocable getVocable(int index) {
        return vocab.get(index);
    }

    public void addNativeWord(String pNativeWord) {
        v.addNativeWords(pNativeWord);
    }

    public void addTranslation(String pTranslation) {
        v.addTranslation(pTranslation);
    }

    public void writeJSON(String fileName) {
        try {
            JSONObject jRoot = new JSONObject();
            jRoot.put("Revision", 1);

            JSONArray jVocab = new JSONArray();
            Iterator it = vocab.iterator();
            while (it.hasNext()) {
                Vocable v = (Vocable) it.next();
                JSONObject jVocable = new JSONObject();
                JSONObject jVocabInfo = new JSONObject();

                JSONArray nativeWords = new JSONArray();
                for (String s : v.getNativeWords()) {
                    nativeWords.put(s);
                }

                JSONArray translations = new JSONArray();
                for (String s : v.getTranslations()) {
                    translations.put(s);
                }

                jVocabInfo.put("NativeWords", nativeWords);
                jVocabInfo.put("Translations", translations);
                jVocabInfo.put("TimesCorrect", v.getTimesCorrect());

                jVocab.put(jVocabInfo);
            }

            jRoot.put("Vocab", jVocab);
            String json = jRoot.toString();
            BufferedWriter file = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName+".json"), "UTF8"));
            file.write(json);
            file.flush();
            file.close();
        } catch (Exception e) {
        }
    }
    
    /*public void writeFile(String fileName) {
        if (!vocab.isEmpty()) {
            createDocument();
            createDOMTree();
            saveToFile(fileName);
        }
    }

    public void createDocument() {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            dom = db.newDocument();
        } catch (ParserConfigurationException pce) {
            System.out.println(pce);
        }
    }

    public void createDOMTree() {
        Element rootEle = dom.createElement("List");
        dom.appendChild(rootEle);

        Element revisionEle = dom.createElement("Revision");
        Text revisionText = dom.createTextNode("1");
        revisionEle.appendChild(revisionText);
        rootEle.appendChild(revisionEle);

        Iterator it = vocab.iterator();
        while (it.hasNext()) {
            Vocable v = (Vocable) it.next();
            Element vocEle = createVocabElement(v);
            rootEle.appendChild(vocEle);
        }
    }

    public Element createVocabElement(Vocable v) {
        Element vocEle = dom.createElement("Vocable");
        vocEle.setAttribute("required", String.valueOf(v.getRequired()));
        vocEle.setAttribute("type", v.getType());

        Element nativeWord = dom.createElement("NativeWord");
        Text nativeText = dom.createTextNode(v.getNativeWord());
        nativeWord.appendChild(nativeText);
        vocEle.appendChild(nativeWord);

        Element translation = dom.createElement("Translation");
        Text translationText = dom.createTextNode(v.getTranslation());
        translation.appendChild(translationText);
        vocEle.appendChild(translation);

        Element alternativeTranslation = dom.createElement("AlternativeTranslation");
        Text alternativeText = dom.createTextNode(v.getAlternativeTranslation());
        alternativeTranslation.appendChild(alternativeText);
        vocEle.appendChild(alternativeTranslation);

        return vocEle;
    }

    public void saveToFile(String fileName) {
        try {
            OutputFormat format = new OutputFormat(dom);
            format.setIndenting(true);

            XMLSerializer serializer = new XMLSerializer(new FileOutputStream(new File(fileName + ".xml")), format);
            serializer.serialize(dom);
        } catch (Exception e) {
            System.out.println(e);
        }
    }*/
}
